package com.zxq.common.utils.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;


/**
 * 通用错误码枚举类
 */
@Getter
@AllArgsConstructor
public enum ErrorCode {

    SUCCESS(0, "成功", AlarmLevel.NO_ALARM),
    LOGIN_FAILED(5, "登录失效", AlarmLevel.LEVEL_P1),
    PERMISSION_ERROR(12, "权限异常", AlarmLevel.LEVEL_P1),
    PARAM_ERROR(400, "参数错误", AlarmLevel.NO_ALARM),
    SERVER_INTERNAL_ERROR(500, "服务内部异常", AlarmLevel.LEVEL_P1),

    /*-----------------------特殊异常（9000~9999）-----------------------*/
    IDEMPOTENT_INTERCEPTION(9999, "幂等拦截异常，requestId已经被处理过，不再重复处理", AlarmLevel.NO_ALARM),
    LOCK_FAIL_EXCEPTION(9980, "加锁失败", AlarmLevel.NO_ALARM),

    /*-----------------------通用异常（1000~1999）-----------------------*/
    BUSINESS_ERROR(1000, "业务异常", AlarmLevel.LEVEL_P1),
    ENUM_NOT_FOUND_EXCEPTION(1100, "未查询到枚举值", AlarmLevel.LEVEL_P1),

    /*-----------------------应用层异常（2000~2999）-----------------------*/
    USER_NOT_FOUNT_EXCEPTION(2101, "用户不存在", AlarmLevel.NO_ALARM),

    CASH_ACCOUNT_NOT_EXIST_EXCEPTION(2201, "现金账户不存在", AlarmLevel.LEVEL_P1),

    ACTIVITY_NOT_EXIST_EXCEPTION(2301, "活动不存在", AlarmLevel.LEVEL_P1),
    ACTIVITY_ILLEGALITY_EXCEPTION(2302, "活动设置有误，不允许流转", AlarmLevel.LEVEL_P1),
    ACTIVITY_TYPE_INVALID_EXCEPTION(2303, "活动类型不合法", AlarmLevel.LEVEL_P1),
    ACTIVITY_REDEEM_EXCEPTION(2304, "活动兑换失败", AlarmLevel.LEVEL_P1),

    COLLECT_CARD_ADD_EXCEPTION(2401, "收藏卡片失败", AlarmLevel.LEVEL_P1),
    COLLECT_CARD_REMOVE_EXCEPTION(2402, "移除卡片失败", AlarmLevel.LEVEL_P1),
    COLLECT_CARD_NOT_EXIST_EXCEPTION(2403, "收藏卡片不存在", AlarmLevel.LEVEL_P1),

    /*-----------------------领域层异常（3000~3999）-----------------------*/
    USER_TYPE_INVALID_EXCEPTION(3101, "无效的UserType", AlarmLevel.LEVEL_P1),
    USER_INVALID_EXCEPTION(3102, "user信息不合法", AlarmLevel.NO_ALARM),

    CASH_ACCOUNT_WITHDRAW_EXCEPTION(3201, "提现异常", AlarmLevel.LEVEL_P0),

    ACTIVITY_REWARD_CONDITION_NOT_EXIST(3301, "活动没有设置奖励兑换条件", AlarmLevel.LEVEL_P1),
    NOT_MEET_REWARD_CONDITION(3302, "不满足兑奖条件", AlarmLevel.LEVEL_P1),
    ACTIVITY_HAS_ENDED(3303, "活动已结束", AlarmLevel.LEVEL_P1),
    ALL_REWARDS_HAVE_BEEN_REDEEMED(3304, "来晚啦，奖励已经全部被兑换", AlarmLevel.LEVEL_P1),

    ;


    private final Integer code;
    private final String message;
    private final AlarmLevel alarmLevel;

    public boolean isSucceed() {
        return Objects.equals(getCode(), SUCCESS.getCode());
    }

    public boolean isAlarm() {
        return AlarmLevel.needAlarm(alarmLevel);
    }

    public static ErrorCode getByCode(Integer code) {
        for (ErrorCode errorCode : ErrorCode.values()) {
            if (errorCode.getCode().equals(code)) {
                return errorCode;
            }
        }
        return null;
    }




}
